package dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;
import java.util.HashMap;
import entidade.Categoria;

public class CategoriaDAO {

	private Connection connection;
	
	public CategoriaDAO() throws SQLException
	{
		connection = ConnectionFactory.getConnection();
		
	}
	
	public void addNewCategoria(Categoria categoria) throws SQLException
	{
		PreparedStatement stmt = connection.prepareStatement("insert into tipo_revista(Tipo) values(?)");
		stmt.setString(1, categoria.getTipo());
		stmt.execute();
		stmt.close();
	}
	
	public HashMap<String, Long> getCategorias() throws SQLException
	{
		PreparedStatement stmt = connection.prepareStatement("select * from tipo_revista");
		ResultSet rs = stmt.executeQuery();
		HashMap<String, Long> categorias = new HashMap<String, Long>();
		categorias.put("--Todos--", new Long(0));
		while (rs.next())
		{			
			categorias.put(rs.getString("Tipo"), new Long(rs.getLong("IDTipoRevista")));
		}
		return categorias;		
	}


}
